package com.yabushan.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.yabushan.common.utils.StringUtils;
import com.yabushan.system.domain.YmxGiftInfo;
import com.yabushan.system.domain.vo.YmxGifiTemplateInfoVo;
import com.yabushan.system.domain.vo.YmxGiftTemplateInfoExcelVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yabushan.common.annotation.Log;
import com.yabushan.common.core.controller.BaseController;
import com.yabushan.common.core.domain.AjaxResult;
import com.yabushan.common.enums.BusinessType;
import com.yabushan.system.domain.YmxTemplateInfo;
import com.yabushan.system.service.IYmxTemplateInfoService;
import com.yabushan.common.utils.poi.ExcelUtil;
import com.yabushan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 礼物模板Controller
 *
 * @author yabushan
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/ymx/ymxtemplateInfo")
public class YmxTemplateInfoController extends BaseController {
    @Autowired
    private IYmxTemplateInfoService ymxTemplateInfoService;

    /**
     * 查询礼物模板列表
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(YmxTemplateInfo ymxTemplateInfo) {
        startPage();
        List<YmxGifiTemplateInfoVo> list = ymxTemplateInfoService.selectYmxTemplateInfoList(ymxTemplateInfo);
        return getDataTable(list);
    }

    /**
     * 查询礼物模板名字列表
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:mapName')")
    @GetMapping("/mapName")
    public Map<String, String> mapName() {
        startPage();
        Map<String, String> map = ymxTemplateInfoService.selectYmxTemplateInfomapName();
        return map;
    }


    /**
     * 导出礼物模板列表
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:export')")
    @Log(title = "礼物模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YmxTemplateInfo ymxTemplateInfo) {
        List<YmxGifiTemplateInfoVo> list = ymxTemplateInfoService.selectYmxTemplateInfoList(ymxTemplateInfo);
        ExcelUtil<YmxGifiTemplateInfoVo> util = new ExcelUtil<YmxGifiTemplateInfoVo>(YmxGifiTemplateInfoVo.class);
        return util.exportExcel(list, "YmxGifiTemplateInfoVo");
    }

    /**
     * 获取礼物模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") String templateId) {
        return AjaxResult.success(ymxTemplateInfoService.selectYmxTemplateInfoById(templateId));
    }

    /**
     * 新增礼物模板
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:add')")
    @Log(title = "礼物模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YmxGifiTemplateInfoVo ymxGifiTemplateInfoVo) {
        return toAjax(ymxTemplateInfoService.insertYmxTemplateInfo(ymxGifiTemplateInfoVo));
    }

    /**
     * 修改礼物模板对应礼物
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:update')")
    @Log(title = "礼物模板", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody YmxGifiTemplateInfoVo ymxTemplateInfo) {
        return toAjax(ymxTemplateInfoService.updateYmxTemplateByGiftInfo(ymxTemplateInfo));
    }

    /**
     * 修改礼物模板
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:edit')")
    @Log(title = "礼物模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YmxTemplateInfo ymxTemplateInfo) {
        return toAjax(ymxTemplateInfoService.updateYmxTemplateInfo(ymxTemplateInfo));
    }

    /**
     * 删除礼物模板
     */
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:remove')")
    @Log(title = "礼物模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable String[] templateIds) {
        return toAjax(ymxTemplateInfoService.deleteYmxTemplateInfoByIds(templateIds));
    }

    @Log(title = "批量导入礼物信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ymx:ymxtemplateInfo:importDataGift')")
    @PostMapping("/importDataGift")
    @ApiOperation(value = "批量导入礼物信息")
    public AjaxResult importDataGift(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<YmxGiftTemplateInfoExcelVo> util = new ExcelUtil<YmxGiftTemplateInfoExcelVo>(YmxGiftTemplateInfoExcelVo.class);
        List<YmxGiftTemplateInfoExcelVo> userList = util.importExcel(file.getInputStream());
        List<String> list = new ArrayList<>();
        boolean equals = false;
        int size = userList.size();
        if (size != 0 ){
            for (int i = 0; i < size; i++) {
                YmxGiftTemplateInfoExcelVo excelVo = userList.get(i);
                if (!StringUtils.isEmpty(excelVo.getTemplateName())) {
                    String templateName = excelVo.getTemplateName();
                    List<YmxGiftTemplateInfoExcelVo> resultList = userList.stream().filter(student -> Objects.equals(student.getTemplateName(),templateName)).collect(Collectors.toList());
                    YmxGifiTemplateInfoVo templateInfoVo = new YmxGifiTemplateInfoVo();
                    List<YmxGiftInfo> giftInfoList = new ArrayList<>();
                    int size1 = resultList.size();
                    if (size1 != 0) {
                        for (int j = 0; j < size1; j++) {
                            YmxGiftInfo giftInfo = new YmxGiftInfo();
                            YmxGiftTemplateInfoExcelVo infoExcelVo = resultList.get(j);
                            giftInfo.setGiftName(infoExcelVo.getGiftName());
                            giftInfo.setGiftImage(infoExcelVo.getGiftImage());
                            giftInfoList.add(giftInfo);
                        }
                    }
                    templateInfoVo.setTemplateName(templateName);
                    templateInfoVo.setGiftInfoList(giftInfoList);
                    List<String> myList = list.stream().distinct().collect(Collectors.toList());
                    int size2 = myList.size();
                    if (size2 != 0) {
                        for (int j = 0; j < size2; j++) {
                            String string = myList.get(j);
                            equals = string.equals(templateName);
                            if (equals) {
                                break;
                            }
                        }
                    }
                    if (!equals) {
                        ymxTemplateInfoService.insertYmxTemplateInfo(templateInfoVo);
                    }
                    list.add(templateName);
                }
            }
        }
        // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //String operName = loginUser.getUsername();
        //String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success("批量导入礼物信息");
    }
}
